import CONSTANT from '@/common/constants.js';
import http from '@/common/http.js'
import XInput from 'vux/src/components/x-input/index.vue'
import Group from 'vux/src/components/group/index.vue'
import Datetime from 'vux/src/components/datetime/index.vue'
import Picker from 'vux/src/components/picker/index.vue'
import Popup from 'vux/src/components/popup/index.vue'
import util from '@/common/util.js'

var siteIda = global.siteId
var list = []
http.get(CONSTANT.DICTLIST + '?orgId=' + siteIda + '&type=edu_grade', {}, (res) => {
  // 获取年级段
  let data = res.data
  for (var i = 0; i < data.length; i++) {
    var listOne = {}
    //if (res[i].resName != '幼儿') {
      listOne.name = data[i].label
      listOne.value = data[i].value
      list.push(listOne)
    //}
  }
})
export default {
  name: 'modifyInformation',
  components: {XInput, Group, Datetime, Picker, Popup},
  data () {
    return {
      imageHttp: CONSTANT.uploadProfilePic,
      minYear: 1900,
      maxYear: 2017,
      show: false,
      gradeList: [list],
      gradeListValue: [],
      siteId: '',
      studentId: '',
      avatar:'',
      bspId: '',
      fileId: '',
      modifyBtn: 'modifyBtn',
      isDisabled: false,
      avatarExist: false,
      avatarShow: false,
      idCardNeeds: true,
      apiArea: '',
      name: '',
      school: '',
      mobile: '',
      numID: '',
      classNum: '',
      birthday: '',
      sex: '男',
      uploadData: {
        userId: ''
      },
      sfzyanzhen: function (value) {
        let r = /^(\d{18}|\d{17}x)$/;
        if (r.test(value)) {
          return {
            valid: true,
            msg: '身份证正确'
          }
        } else {
          return {
            valid: false,
            msg: '身份证错误'
          }
        }
      }
    }
  },
  mounted () {
    var _this = this
    _this.maxYear = new Date().getFullYear()
    _this.siteId = window.sessionStorage.getItem('siteId')
    _this.studentId = window.sessionStorage.getItem('studentId')
    _this.uploadData.userId = window.sessionStorage.getItem('studentId')
    _this.dataInit()
    http.get(CONSTANT.SETTING + '?siteId=' + _this.siteId + '&', {}, (res) => {
      // 获取身份证是否是必填项
      console.log(res)
      _this.idCardNeeds = res.idCardNeeds
      console.log(_this.idCardNeeds)
    })
    http.post(CONSTANT.STUDENTVIEW, {
      orgId: _this.siteId,
      userId: _this.studentId
    }, (res) => {
      console.log(res)
      let data = res.data
      _this.name = data.name
      _this.mobile = data.mobile
      _this.numID = data.cardNo
      _this.school = data.school
      _this.gradeListValue[0] = data.grade
      _this.birthday = data.birthday
      if (data.birthday != '') {
        _this.birthday = util.dateFormat(data.birthday, false)
      }
      _this.sex = data.sex
      if (_this.sex == '0') {
        _this.sex = '男'
        this.$refs.man.className = 'iconfont icon-danxuan-xuanzhong iconfontChecked'
        this.$refs.woman.className = 'iconfont icon-danxuan1 iconfontNotChecked'
      } else {
        _this.sex = '女'
        this.$refs.woman.className = 'iconfont icon-danxuan-xuanzhong iconfontChecked'
        this.$refs.man.className = 'iconfont icon-danxuan1 iconfontNotChecked'
      }
      if (_this.avatarShow) {
        http.get(CONSTANT.getProfilePic + '?userId=' + _this.studentId + '&', {}, (res) => {
          _this.avatar = res.data.profilePicUrl
          _this.bspId = res.data.bspId
        })
      }
    })
  },
  methods: {
    dataInit() {
      let _this = this
      if (_this.siteId == '2C9146A12760F52A01276124EFB90016') {//宁波
        _this.avatarShow = true
        _this.apiArea = 'nb'
      } else if (_this.siteId == '00006e72043a7f7243629eb4e1904f42c53f') {//省团校
        _this.avatarShow = true
        _this.apiArea = 'tx'
      }
    },
    overModify () {
      console.log(this)
      var _this = this
      if (!_this.isDisabled) {
        _this.isDisabled = true
        _this.modifyBtn = 'modifyBtnDisable'
        var sexs = 0
        if (_this.sex == '男') {
          sexs = 0
        } else {
          sexs = 1
        }
        if (_this.name == '') {
          alert('请输入您的姓名！')
        } else if (_this.mobile == '') {
          alert('请输入您的手机号码！')
        } else if (_this.$refs.tel.valid == false) {
          alert('请输入正确的手机号码！')
        } else if (_this.numID == '' && _this.idCardNeeds == true) {
          alert('请输入您的身份证号码！')
        } else if (_this.$refs.idcard.valid == false) {
          alert('请输入正确的身份证号码！')
        } else if (_this.birthday == '') {
          alert('请选择出生日期！')
        } else if (_this.gradeListValue[0] == '-1') {
          alert('请选择年级！')
        } else {
          http.post(CONSTANT.STUDENTUPDATE, {
            userId: _this.studentId,
            orgId: _this.siteId,
            name: _this.name,
            mobile: _this.mobile,
            cardNo: _this.numID,
            grade: _this.gradeListValue[0],
            sex: sexs,
            birthday: _this.birthday,
            school: _this.school
          }, (res) => {
            /* console.log(res)
             if (res.msg) {
             alert(res.msg)
             }
             window.history.go(-1)*/
            if (_this.avatarShow) {
              if (_this.fileId != null && _this.fileId != '') {
              //_this.bspId = ''
              let url = ''
              if (_this.bspId != null && _this.bspId != '') {
                url = "http://47.99.107.170:8181/qsng-api/api/"+_this.apiArea+"/updateUser?personNo=" + _this.studentId.substr(0,10) + "&personName=" + _this.name + "&cardNo=" + _this.numID + "&facePhoto=" + _this.avatar + '&personId=' + _this.bspId
              } else {
                url = "http://47.99.107.170:8181/qsng-api/api/"+_this.apiArea+"/addUser?personNo=" + _this.studentId.substr(0,10) + "&personName=" + _this.name + "&cardNo=" + _this.numID + "&facePhoto=" + _this.avatar
              }
              http.get(url, {}, (res2) => {
                if (res2.data.errCode != 0) {
                alert('人脸头像上传失败')
                return
              } else {
                _this.bspId = res2.data.personId
                _this.setBspId()
              }
            })
            } else {
              if (res.msg) {
                alert(res.msg)
              }
              window.history.go(-1)
            }
          } else {
            if (res.msg) {
              alert(res.msg)
            }
            window.history.go(-1)
          }
        })
        }
      }
    },
    setBspId() {
      let _this = this
      http.get(CONSTANT.setBspId + '?id=' + _this.fileId + '&bspId=' + _this.bspId, {}, (res) => {
        if (res.code == 0) {
          alert('更新成功')
          window.history.go(-1)
        } else {
          alert('更新失败')
        }
      })
    },
    chooseOK () {
      var _this = this
      _this.show = false
    },
    checkone (e) {
      // 男选择按钮
      var _this = this
      if (e.srcElement.className == 'iconfont icon-danxuan-xuanzhong iconfontChecked') {
        e.target.parentElement.children[1].className = 'iconfont icon-danxuan1 iconfontNotChecked'
        _this.sex = '男'
      } else {
        e.srcElement.className = 'iconfont icon-danxuan-xuanzhong iconfontChecked'
        e.target.parentElement.children[1].className = 'iconfont icon-danxuan1 iconfontNotChecked'
        _this.sex = '男'
      }
      console.log(this.sex)
    },
    checktwo (e) {
      // 女选择按钮
      var _this = this
      if (e.srcElement.className == 'iconfont icon-danxuan-xuanzhong iconfontChecked') {
        e.target.parentElement.children[0].className = 'iconfont icon-danxuan1 iconfontNotChecked'
        _this.sex = '女'
      } else {
        e.srcElement.className = 'iconfont icon-danxuan-xuanzhong iconfontChecked'
        e.target.parentElement.children[0].className = 'iconfont icon-danxuan1 iconfontNotChecked'
        _this.sex = '女'
      }
      console.log(this.sex)
    },
    comeBack () {
      window.history.go(-1)
    },
    handleAvatarSuccessz(res, file) {
      console.log(res)
      this.avatar = res.data.url
      this.fileId = res.data.id
    },
    beforeAvatarUpload(file) {
      let size = file.size / 1024 / 1024
      if (size > 10) {
          alert('图片不能大于10M')
          return false
      }
    },
    handleAvatarError () {
      alert('上传失败,请重新上传！')
    },
    handleAvatarPorgress () {
      console.log('正在上传')
    }
  }
}
